package com.vfa.vocaburay;

import java.util.ArrayList;

import com.vfa.data.Category;
import com.vfa.data.CategoryData;
import com.vfa.data.Language;
import com.vfa.data.LanguageData;
import com.vfa.data.WordData;
import com.vfa.data.toastMaking;

import android.R.string;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class updateWord extends Activity implements OnClickListener {
	/** Called when the activity is first created. */
	private Button updateWordButton;
	private Button clearWordButton;
	private EditText wordEdt;
	private EditText meanEdt;
	private EditText exampleEdt;
	private EditText readEdt;
	private Spinner speechSpinner;
	private  Spinner languageSpinner;
    private ArrayAdapter<Category> speechAdapter;
    private ArrayAdapter<Language> languageAdapter;
    private ArrayList<Category>arrCategoryName = new ArrayList<Category>();
    private ArrayList<Language> arrLanguage = new ArrayList<Language>();
    private CategoryData category;
    private LanguageData languageData;
//	WordData wordData;
	private int wordID;
	Typeface font;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.editwords);
		//get data
		  Bundle result = getIntent().getExtras();
		  String wordName = result.getString("wordName");
		  String wordMeaning  = result.getString("wordMeaning");
		  String wordExample = result.getString("wordExample");
		  String wordCategory = result.getString("wordCategory");
		  String wordLanguage = result.getString("wordLanguage");
		  String wordRead = result.getString("wordRead");
		  wordID = result.getInt("wordID");
		  initCategoryAndLanguage();
	      getAllCategory();
	      getAllLanguage();
		//
	      font = Typeface.createFromAsset(getAssets(), "ariali.ttf");  
		updateWordButton =  (Button) findViewById(R.id.UpdateWordButton);
		clearWordButton =  (Button) findViewById(R.id.ClearUpdateWordButton);
		wordEdt = (EditText) findViewById(R.id.UpdatewordContent);
		meanEdt = (EditText)	 findViewById(R.id.UpdatewordMeaning);
		exampleEdt = (EditText) findViewById(R.id.UpdatewordExample);
		readEdt = (EditText) findViewById(R.id.UpdateWordRead);
		readEdt.setTypeface(font);
//		vfaMain.wordData = new WordData(getApplicationContext());
		 speechSpinner = (Spinner) findViewById(R.id.UpdateCatagorySpinner);
		 speechAdapter = new ArrayAdapter<Category>(this, android.R.layout.simple_spinner_item, arrCategoryName);
	        //ArrayAdapter<String> speechAdapter = new ArrayAdapter<String>(this, R.layout.listiterm, categoryInit);
	        speechAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	        speechSpinner.setVisibility(1);
	        speechSpinner.setAdapter(speechAdapter);
		languageSpinner = (Spinner) findViewById(R.id.UpdateLanguageSpinner);
        languageAdapter = new ArrayAdapter<Language>(this, android.R.layout.simple_spinner_item, arrLanguage);
        languageAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        languageSpinner.setVisibility(1);
        languageSpinner.setAdapter(languageAdapter);

		languageSpinner.setAdapter(languageAdapter);
		// fill data
		wordEdt.setText(wordName);
		meanEdt.setText(wordMeaning);
		exampleEdt.setText(wordExample);
		readEdt.setText(wordRead);
		for(int i=0;i<speechSpinner.getCount();i++)
		{
			if(speechSpinner.getItemAtPosition(i).toString().equals(wordCategory))
			{
				speechSpinner.setSelection(i);
				break;
			}			
		}
		
		for(int i=0;i<languageSpinner.getCount();i++)
		{
			if(languageSpinner.getItemAtPosition(i).toString().equals(wordLanguage))
			{
				languageSpinner.setSelection(i);
				break;
			}
		}
		
		
		updateWordButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(wordEdt.getText().toString().equals("") || meanEdt.getText().toString().equals(""))
				{
				 toastMaking.errorToast(getApplicationContext(), "Fill wordname and its meaning").show();	
				}
				else {
				int result  = vfaMain.wordData.update(wordID,wordEdt.getText().toString(), 
						speechSpinner.getSelectedItem().toString(),
						languageSpinner.getSelectedItem().toString() , 
						meanEdt.getText().toString(), 
						exampleEdt.getText().toString(),readEdt.getText().toString());
				if(result > 0){
					Toast.makeText(getBaseContext(), "Update "+ wordEdt.getText()+"  successful!", Toast.LENGTH_SHORT).show();
				}else{
					Toast.makeText(getBaseContext(), "Update "+ wordEdt.getText()+"  failed! :(", Toast.LENGTH_SHORT).show();
				}
					
				// move back to wordList
				
				Intent intent = new Intent(updateWord.this, WordList.class);
				startActivity(intent);
				updateWord.this.finish();	
				}
			}
		});
		
		clearWordButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(updateWord.this, WordList.class);
    			startActivity(intent);
    			updateWord.this.finish();		
			}
		});
		 
	}


	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}
//	@Override
//	public void onBackPressed() {
//		Intent intent = new Intent(updateWord.this, WordList.class);
//		startActivity(intent);
//		updateWord.this.finish();		
//	}
	private void getAllCategory(){
	   	
		arrCategoryName = category.getDatabase();
        System.out.println(arrCategoryName.size());
    }
    private void getAllLanguage(){
       	
    	arrLanguage.clear();
    	arrLanguage = languageData.getAllLangguage();

    }
    private void initCategoryAndLanguage(){
        category =  new CategoryData(getApplicationContext());
        languageData = new LanguageData(getApplicationContext());
    }
}